/* $RCSFile: ADNDynCampaign.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/CRMWrapper3/DynEntities/ADNDynCampaign.cs $
 * $log$
 * Revision 19 2010/10/02 14:01:48  christian.surieux
 *   Corrected a bug in allmost everyADNDyn file for PrimaryFieldVualue
 * Revision 6 2010/08/06 19:56:32  christian.surieux
 *   Added Log to All cs files
 */

/* Copyrights Christian Surieux 2010 */
/* Class ADNDynCampaign */
/* Date 2010-01-06 15:58:16Z */

using System;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

using CrmSdk;
using System.Data;

namespace AddonNice.CRMWrapper
{
	#region ADNDynCampaign TraceSwitch for CRM
	public class DynCampaignTrace
	{
		static public ADNTraceSwitchW	Sw	=	new ADNTraceSwitchW("DynCampaignTrace","Trace Switch for CRM ADNDynCampaign");
	}
	#endregion ADNDynCampaign TraceSwitch for CRM

    public class ADNDynCampaign : DynEntity
    {
        protected class Ftor: DFactor
        {
            public Ftor() : base(CrmStr.campaign)
            {}

            public override Type DynType
            { get { return typeof(ADNDynCampaign); } }

            public override string DynTypename
            { get { return CrmStr.campaign; } }

            public override DynEntity Build()
            {   return new ADNDynCampaign();}

            public override DynEntity Build(CRMWrapperBase w, InternalDynamicEntity dEntity, PropertyDescriptorCollection wproperties)
            {   return new ADNDynCampaign(w, dEntity, wproperties, null);}

            public override DynEntity Build(CRMWrapperBase w, string PrimaryFieldValue)
            {   return new ADNDynCampaign(w,PrimaryFieldValue);}

            public override DynEntity Build(CRMWrapperBase w,Guid PrimaryKeyValue)
            {   return new ADNDynCampaign(w,PrimaryKeyValue);}
        }

        static ADNDynCampaign(){new Ftor();} 
        
        public override bool HasOwningBusinessUnitProperty
        { get { return true; } }
        
        public ADNDynCampaign() : base() { }
        
        public ADNDynCampaign(CRMWrapperBase w,string PrimaryFieldValue)
            : base(w,CrmStr.campaign)
        {
            if ( PrimaryFieldValue != string.Empty )
                name = PrimaryFieldValue;
        }
        
        public ADNDynCampaign(CRMWrapperBase w,Guid CrmId) : base(w,CrmStr.campaign,CrmId) { }
        
        public ADNDynCampaign(CRMWrapperBase w,InternalDynamicEntity e) : base(w,CrmStr.campaign,e,null,null) { }
        
        public ADNDynCampaign(CRMWrapperBase w,InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s) : base(w,CrmStr.campaign,e,prop,s) { }
        
        public ADNDynCampaign(ResetWrapperHandler fn,string PrimaryFieldValue)
            : base(fn,CrmStr.campaign)
        {
            if ( PrimaryFieldValue != string.Empty )
                name = PrimaryFieldValue;
        }
        
        public ADNDynCampaign(ResetWrapperHandler fn,Guid CrmId) : base(fn,CrmStr.campaign,CrmId) { }
        
        public ADNDynCampaign(ResetWrapperHandler fn,InternalDynamicEntity e) : base(fn,CrmStr.campaign,e,null,null) { }
        
        public ADNDynCampaign(ResetWrapperHandler fn,InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s) : base(fn,CrmStr.campaign,e,prop,s) { }

        /* Properties */

        [CrmDynAttribute(CrmStr.campaign)]
        public Guid campaignid
        {
            get
            { return KeyPropertyValue(CrmStr.campaignid); }
            set
            { AddKeyProperty(CrmStr.campaignid,value); }
        }

        protected override void SetCrmIdProperty(Guid crmId)
        {
            if ( !dicProperty.ContainsKey(CrmStr.campaignid) || KeyPropertyValue(CrmStr.campaignid) != crmId )
                campaignid = crmId;
        }

        public override Guid PrimaryKeyGuidValue
        {
            get
            { return campaignid; }
            set { campaignid = value; }
        }

        public override string PrimaryFieldName
        {
            get{ return CrmStr.name; }
            set{}
        }

        /// <summary>
        /// Value contained in the PrimaryField for the DynEntity or empty if no PrimaryField
        /// </summary>
        public override string PrimaryFieldStringValue
        {
            get
            {
                return name;
            }
            set
            {
                name =   value;
            }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public override string name
        {
            get{ return StringPropertyValue(CrmStr.name); }
            set{ AddStringProperty(CrmStr.name,value); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public string actualend
        {
            get{ return DateTimePropertyValue(CrmStr.actualend); }
            set{ AddDateTimeProperty(CrmStr.actualend,value); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public string actualstart
        {
            get{ return DateTimePropertyValue(CrmStr.actualstart); }
            set{ AddDateTimeProperty(CrmStr.actualstart,value); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public decimal budgetedcost
        {
            get{ return MoneyPropertyValue(CrmStr.budgetedcost); }
            set{ AddMoneyProperty(CrmStr.budgetedcost,value); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public decimal budgetedcost_base
        {
            get{ return MoneyPropertyValue(CrmStr.budgetedcost_base); }
            set{ AddMoneyProperty(CrmStr.budgetedcost_base,value); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public string codename
        {
            get{ return StringPropertyValue(CrmStr.codename); }
            set{ AddStringProperty(CrmStr.codename,value); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public string description
        {
            get{ return StringPropertyValue(CrmStr.description); }
            set{ AddStringProperty(CrmStr.description,value); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public decimal exchangerate
        {
            get{ return DecimalPropertyValue(CrmStr.exchangerate); }
            set{ AddDecimalProperty(CrmStr.exchangerate,value); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public int expectedresponse
        {
            get{ return NumberPropertyValue(CrmStr.expectedresponse); }
            set{ AddNumberProperty(CrmStr.expectedresponse,value); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public decimal expectedrevenue
        {
            get{ return MoneyPropertyValue(CrmStr.expectedrevenue); }
            set{ AddMoneyProperty(CrmStr.expectedrevenue,value); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public decimal expectedrevenue_base
        {
            get{ return MoneyPropertyValue(CrmStr.expectedrevenue_base); }
            set{ AddMoneyProperty(CrmStr.expectedrevenue_base,value); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public bool istemplate
        {
            get{ return BooleanPropertyValue(CrmStr.istemplate); }
            set{ AddBooleanProperty(CrmStr.istemplate,value); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public string message
        {
            get{ return StringPropertyValue(CrmStr.message); }
            set{ AddStringProperty(CrmStr.message,value); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public string objective
        {
            get{ return StringPropertyValue(CrmStr.objective); }
            set{ AddStringProperty(CrmStr.objective,value); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public decimal othercost
        {
            get{ return MoneyPropertyValue(CrmStr.othercost); }
            set{ AddMoneyProperty(CrmStr.othercost,value); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public decimal othercost_base
        {
            get{ return MoneyPropertyValue(CrmStr.othercost_base); }
            set{ AddMoneyProperty(CrmStr.othercost_base,value); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public Guid owningbusinessunit
        {
            get
            { return LookupPropertyValue(CrmStr.owningbusinessunit); }
            set
            { AddLookupProperty(CrmStr.owningbusinessunit,CrmStr.businessunit,value); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public Guid owninguser
        {
            get{ return LookupPropertyValue(CrmStr.owninguser); }
            set{ AddLookupProperty(CrmStr.owninguser,CrmStr.systemuser,value); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public Guid pricelistid
        {
            get{ return LookupPropertyValue(CrmStr.pricelistid); }
            set{ AddLookupProperty(CrmStr.pricelistid,CrmStr.pricelevel,value); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public string pricelistName
        {
            get{ return LookupPropertyName(CrmStr.pricelistid); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public string promotioncodename
        {
            get{ return StringPropertyValue(CrmStr.promotioncodename); }
            set{ AddStringProperty(CrmStr.promotioncodename,value); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public string proposedend
        {
            get{ return DateTimePropertyValue(CrmStr.proposedend); }
            set{ AddDateTimeProperty(CrmStr.proposedend,value); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public string proposedstart
        {
            get{ return DateTimePropertyValue(CrmStr.proposedstart); }
            set{ AddDateTimeProperty(CrmStr.proposedstart,value); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public int timezoneruleversionnumber
        {
            get{ return NumberPropertyValue(CrmStr.timezoneruleversionnumber); }
            set{ AddNumberProperty(CrmStr.timezoneruleversionnumber,value); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public decimal totalactualcost
        {
            get{ return MoneyPropertyValue(CrmStr.totalactualcost); }
            set{ AddMoneyProperty(CrmStr.totalactualcost,value); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public decimal totalactualcost_base
        {
            get{ return MoneyPropertyValue(CrmStr.totalactualcost_base); }
            set{ AddMoneyProperty(CrmStr.totalactualcost_base,value); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public decimal totalcampaignactivityactualcost
        {
            get{ return MoneyPropertyValue(CrmStr.totalcampaignactivityactualcost); }
            set{ AddMoneyProperty(CrmStr.totalcampaignactivityactualcost,value); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public decimal totalcampaignactivityactualcost_base
        {
            get{ return MoneyPropertyValue(CrmStr.totalcampaignactivityactualcost_base); }
            set{ AddMoneyProperty(CrmStr.totalcampaignactivityactualcost_base,value); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public Guid transactioncurrencyid
        {
            get{ return LookupPropertyValue(CrmStr.transactioncurrencyid); }
            set{ AddLookupProperty(CrmStr.transactioncurrencyid,CrmStr.transactioncurrency,value); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public string transactioncurrencyidName
        {
            get{ return LookupPropertyName(CrmStr.transactioncurrencyid); }
        }

        /// <summary>
        /// Picklist values1 Advertisement
        /// Direct Marketing
        /// Event
        /// Co-branding
        /// Other 
        /// Default value1 Advertisement 
        /// </summary>
        [CrmDynAttribute(CrmStr.campaign)]
        public int typecode
        {
            get{ return PicklistPropertyIntValue(CrmStr.typecode); }
            set{ AddPicklistProperty(CrmStr.typecode,value); }
        }

        [CrmDynAttribute(CrmStr.campaign)]
        public string typecodeLabel
        {
            get{ return PicklistPropertyLabelValue(CrmStr.typecode);  }
        }

        /// <summary>
        /// Build a SortedList 
        /// entName: string.empty for all entities, else the entity
        /// </summary>
        public SortedList<Guid,ADNDynList> ReadAllList(ResetWrapperHandler fn,string entName)
        {
            if (DynCampaignTrace.Sw.TraceVerbose || CRMDynTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNDynCampaign ReadAllList BEG entName: {0}",entName),(DynCampaignTrace.Sw.TraceVerbose)? DynCampaignTrace.Sw.Info:CRMDynTraceSwitch.Sw.Info);
            SortedList<Guid,ADNDynList> Lst                         =   null;
            try
            {
                InternalQueryExpression qe                          =   new InternalQueryExpression();
                qe.EntityName                                       =   CrmStr.list;
                InternalFilterExpression fi                         =   new InternalFilterExpression();
                List<InternalConditionExpression> cLst              =   new List<InternalConditionExpression>();
                if ( !string.IsNullOrEmpty(entName))
                {
                    InternalConditionExpression ex                  =   new InternalConditionExpression();
                    ex.AttributeName                                =   CrmStr.createdfromcode;
                    ex.Operator                                     =   InternalConditionOperator.Equal;
                    switch( entName )
                    {
                        case CrmStr.account:
                        ex.Values                                   =   new object[]{1};
                        break;
                        case CrmStr.contact:
                        ex.Values                                   =   new object[]{2};
                        break;
                        default:
                        case CrmStr.lead:
                        ex.Values                                   =   new object[]{4};
                        break;
                    }
                    cLst.Add(ex);
                }
                if ( cLst.Count > 0 )
                    fi.Conditions                                   =   cLst.ToArray();
                ////
                // link from list to campaignitem 
                InternalLinkEntity le2                              =   new InternalLinkEntity();
                le2.LinkFromEntityName                              =   CrmStr.list;
                le2.LinkFromAttributeName                           =   CrmStr.listid;
                le2.LinkToEntityName                                =   CrmStr.campaignitem;
                le2.LinkToAttributeName                             =   CrmStr.entityid;
                le2.LinkCriteria                                    =   new InternalFilterExpression();
                InternalConditionExpression ex2                     =   new InternalConditionExpression();
                ex2.AttributeName                                   =   CrmStr.entitytype;
                ex2.Operator                                        =   InternalConditionOperator.Equal;
                ex2.Values                                          =   new string[]{CrmStr.list};
                le2.LinkCriteria.Conditions                         =   new InternalConditionExpression[]{ex2};

                // link from campaignitem to campaign
                InternalLinkEntity le                               =   new InternalLinkEntity();
                le.LinkFromEntityName                               =   CrmStr.campaignitem;
                le.LinkFromAttributeName                            =   CrmStr.campaignid;
                le.LinkToEntityName                                 =   CrmStr.campaign;
                le.LinkToAttributeName                              =   CrmStr.campaignid;
                le.LinkCriteria                                     =   new InternalFilterExpression();
                InternalConditionExpression exc                     =   new InternalConditionExpression();
                exc.AttributeName                                   =   CrmStr.campaignid;
                exc.Operator                                        =   InternalConditionOperator.Equal;
                exc.Values                                          =   new string[]{campaignid.ToString()};
                le.LinkCriteria.Conditions                          =   new InternalConditionExpression[]{exc};

                // chain linkentity to linkentity2
                le2.LinkEntities                                    =   new InternalLinkEntity[]{le};

                //add linkentity2 to Query Expression 
                qe.LinkEntities                                     =   new InternalLinkEntity[]{le2};
                ////
                qe.ColumnSet                                        =   new InternalAllColumns();
                qe.Criteria                                         =   fi;
                GenericDynQueryMultiple<ADNDynList> allEntQuery     =   new GenericDynQueryMultiple<ADNDynList>(wrapper,fn,CrmStr.list);
                Lst                                                 =   allEntQuery.retrieveSortedList(qe);
            }
            catch (Exception ex)
            {
                if ( CRMDynTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ADNDynCampaign ReadAllList ex: {0}", ex),CRMDynTraceSwitch.Sw.Info);
                throw;
            }
            if (DynCampaignTrace.Sw.TraceVerbose || CRMDynTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNDynCampaign ReadAllList END Count: {0}",Lst.Count),(DynCampaignTrace.Sw.TraceVerbose)? DynCampaignTrace.Sw.Info:CRMDynTraceSwitch.Sw.Info);
            return Lst;
        }

        /// <summary>
        /// Build a DataTable
        /// entName: string.empty for all entities, else the entity
        /// </summary>
        public DataTable ReadAllListTb(ResetWrapperHandler fn,string entName)
        {
            if (DynCampaignTrace.Sw.TraceVerbose || CRMDynTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNDynCampaign ReadAllListTb BEG entName: {0}",entName),(DynCampaignTrace.Sw.TraceVerbose)? DynCampaignTrace.Sw.Info:CRMDynTraceSwitch.Sw.Info);
            DataTable Tb                                            =   null;
            try
            {
                InternalQueryExpression qe                          =   new InternalQueryExpression();
                qe.EntityName                                       =   CrmStr.list;
                InternalFilterExpression fi                         =   new InternalFilterExpression();
                List<InternalConditionExpression> cLst              =   new List<InternalConditionExpression>();
                if ( !string.IsNullOrEmpty(entName))
                {
                    InternalConditionExpression ex                  =   new InternalConditionExpression();
                    ex.AttributeName                                =   CrmStr.createdfromcode;
                    ex.Operator                                     =   InternalConditionOperator.Equal;
                    switch( entName )
                    {
                        case CrmStr.account:
                        ex.Values                                   =   new object[]{1};
                        break;
                        case CrmStr.contact:
                        ex.Values                                   =   new object[]{2};
                        break;
                        default:
                        case CrmStr.lead:
                        ex.Values                                   =   new object[]{4};
                        break;
                    }
                    cLst.Add(ex);
                }
                if ( cLst.Count > 0 )
                    fi.Conditions                                   =   cLst.ToArray();
                ////
                // link from list to campaignitem 
                InternalLinkEntity le2                              =   new InternalLinkEntity();
                le2.LinkFromEntityName                              =   CrmStr.list;
                le2.LinkFromAttributeName                           =   CrmStr.listid;
                le2.LinkToEntityName                                =   CrmStr.campaignitem;
                le2.LinkToAttributeName                             =   CrmStr.entityid;
                le2.LinkCriteria                                    =   new InternalFilterExpression();
                InternalConditionExpression ex2                     =   new InternalConditionExpression();
                ex2.AttributeName                                   =   CrmStr.entitytype;
                ex2.Operator                                        =   InternalConditionOperator.Equal;
                ex2.Values                                          =   new string[]{CrmStr.list};
                le2.LinkCriteria.Conditions                         =   new InternalConditionExpression[]{ex2};

                // link from campaignitem to campaign
                InternalLinkEntity le                               =   new InternalLinkEntity();
                le.LinkFromEntityName                               =   CrmStr.campaignitem;
                le.LinkFromAttributeName                            =   CrmStr.campaignid;
                le.LinkToEntityName                                 =   CrmStr.campaign;
                le.LinkToAttributeName                              =   CrmStr.campaignid;
                le.LinkCriteria                                     =   new InternalFilterExpression();
                InternalConditionExpression exc                     =   new InternalConditionExpression();
                exc.AttributeName                                   =   CrmStr.campaignid;
                exc.Operator                                        =   InternalConditionOperator.Equal;
                exc.Values                                          =   new string[]{campaignid.ToString()};
                le.LinkCriteria.Conditions                          =   new InternalConditionExpression[]{exc};

                // chain linkentity to linkentity2
                le2.LinkEntities                                    =   new InternalLinkEntity[]{le};

                //add linkentity2 to Query Expression 
                qe.LinkEntities                                     =   new InternalLinkEntity[]{le2};
                qe.ColumnSet                                        =   new InternalAllColumns();
                qe.Criteria                                         =   fi;
                GenericDynQueryMultiple<ADNDynList> allEntQuery     =   new GenericDynQueryMultiple<ADNDynList>(wrapper,fn,CrmStr.list);
                Tb                                                  =   allEntQuery.retrieveDatatable(qe);
            }
            catch (Exception ex)
            {
                if ( CRMDynTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ADNDynCampaign ReadAllListTb ex: {0}", ex),CRMDynTraceSwitch.Sw.Info);
                throw;
            }
            if (DynCampaignTrace.Sw.TraceVerbose || CRMDynTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNDynCampaign ReadAllListTb END Count: {0}",(Tb != null) ? Tb.Rows.Count:0),(DynCampaignTrace.Sw.TraceVerbose)? DynCampaignTrace.Sw.Info:CRMDynTraceSwitch.Sw.Info);
            return Tb;
        }
    }
}